<?php
/**
 * Created by PhpStorm.
 * User: wangan
 * Date: 2016/12/5
 * Time: 15:50
 */

namespace App\Http\Service;

use App\Models\Permission;
use Illuminate\Support\Facades\DB;
class PermissionService
{
    public static function getPermissionListPageInit($pageSize=DEFAULT_PAGESIZE,$where=[],$searchVal=null){
        $entity = Permission::where($where);
        if(!empty($searchVal)){
            return $entity->where('name','like','%'.$searchVal.'%')
                ->paginate($pageSize);
        }
        return $entity->paginate($pageSize);
    }

    public static function createOnePermission($permissionData){
        return Permission::create($permissionData);
    }

    public static function getOnePermission($permissionId){
        return Permission::find($permissionId);
    }

    public static function updateOnePermission($permissionDate){
        return Permission::find($permissionDate['id'])
                    ->update($permissionDate);
    }

    public static function checkHasChild($permissionId){
       return Permission::where('cid', $permissionId)->first();
    }

    public static function deletePermissions($permissionId){
        return Permission::find($permissionId)->delete();
    }

    public static function getAllPermissionGroupby(){
        $permissions = Permission::orderBy('cid','ASC')->get()->toArray();
        $groupResult=[];
        foreach ($permissions as $key => $val){
            if($val['cid'] == 0){
                $groupResult[$val['id']]=$val;
                $groupResult[$val['id']]['childs']=array();
            }
            else{
                $groupResult[$val['cid']]['childs'][]=$val;
            }
        }
        return $groupResult;
    }


}